package com.baichang.taoshunda.service.impl;

import cn.jpush.api.push.model.Message;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.utils.DateUtil;
import com.baichang.framework.utils.JPushHelper;
import com.baichang.taoshunda.dao.ReminderDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.DeliveryClerk;
import com.baichang.taoshunda.entity.Order;
import com.baichang.taoshunda.entity.Reminder;
import com.baichang.taoshunda.entity.em.OrderState;
import com.baichang.taoshunda.service.*;
import com.baichang.taoshunda.utils.UserPushMsgUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/08/25.
 */
@Service
public class ReminderServiceImpl implements ReminderService {
    @Autowired
    private ReminderDao reminderDao;
    @Autowired
    private OrderService orderService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private DeliveryClerkService deliveryClerkService;
    @Autowired
    private ParamsService paramsService;

    /**
     * 催单
     *
     * @param userId     用户编号
     * @param orderId    订单编号
     * @param deliveryId 配送员编号
     * @param bussId
     * @return
     */
    @Override
    public boolean reminderOrder(Integer userId, Integer orderId, Integer deliveryId, Integer bussId) throws ServiceException {
        boolean flag = false;
        //催单次数
        int count = reminderDao.getCount(userId, orderId);
        Order order = null;
        Bussiness bussiness = null;
        DeliveryClerk deliveryClerk = null;


        if (orderId != 0) {
            order = orderService.getOrderMoney(orderId);
            if (order == null) {
                throw new ServiceException("订单不存在");
            }
        }
        if (bussId != 0) {
            bussiness = businessService.getBusinessById(order.getSellerId());
            if (bussiness == null) {
                throw new ServiceException("无当前商家信息");
            }
        }

        if (deliveryId != 0) {
            deliveryClerk = deliveryClerkService.getDeliveryClerkById(order.getDisId());
            if (deliveryClerk == null) {
                throw new ServiceException("无当前配送员信息");
            }
        }
        if (count <= 3) {
            Reminder reminder = new Reminder();
            reminder.setOrderId(orderId);
            reminder.setDeliveryId(deliveryId);
            reminder.setReminderCount(count + 1);
            reminder.setUserId(userId);
            reminder.setBussId(bussId);
            reminder.setBussIsLook(0);
            reminder.setLastTime(new Date());
            if (count == 0) {
                reminderDao.save(reminder);
                if (order.getDispatching().compareTo(BigDecimal.valueOf(0)) > 0) {
                    if (order.getOrderState().equals(OrderState.配送中.getCode()) || order.getOrderState().equals(OrderState.待取货.getCode())) {
                        UserPushMsgUtil.reminderOrderPushByOrderStateToDelivery(orderId, userId, deliveryClerk.getAccount(), order.getOrderNumber(), order.getOrderType(),order.getReason());
                        UserPushMsgUtil.reminderOrderPushByOrderStateToBusiness(orderId, userId, bussiness.getAccount(), order.getOrderNumber(), order.getOrderType());
                    } else {
                        UserPushMsgUtil.reminderOrderPushByOrderStateToBusiness(orderId, userId, bussiness.getAccount(), order.getOrderNumber(), order.getOrderType());
                    }

                } else {
                    UserPushMsgUtil.reminderOrderPushByOrderStateToBusiness(orderId, userId, bussiness.getAccount(), order.getOrderNumber(), order.getOrderType());
                }
                flag = true;
            } else {
                Reminder newreminder = reminderDao.getEntityByUserId(userId, orderId);
                Date date = new Date();
                Date lastTime = DateUtil.addMinutes(newreminder.getLastTime(), Integer.parseInt(paramsService.getParamsValue(30, 28).get("value").toString()));
                if (date.getTime() > lastTime.getTime()) {
                    reminderDao.save(reminder);
                    JSONObject object = JSONObject.fromObject(order);
                    Map<String, String> map = object;
                    if (order.getOrderState() == 3) {
                        UserPushMsgUtil.reminderOrderPushByOrderStateToBusiness(orderId, userId, bussiness.getAccount(), order.getOrderNumber(), order.getOrderType());
                        JPushHelper companyJPushHelper = new JPushHelper("business.properties");
                        companyJPushHelper.sendMsgByAlias(Message.newBuilder().setMsgContent("").addExtras(map).build(), bussiness.getAccount());
                    } else if (order.getOrderState() == 4) {
                        if (order.getDispatching().compareTo(BigDecimal.valueOf(0)) > 0) {
                            UserPushMsgUtil.reminderOrderPushByOrderStateToDelivery(orderId, userId, deliveryClerk.getAccount(), order.getOrderNumber(), order.getOrderType(), order.getReason());
                            JPushHelper jPushHelper = new JPushHelper("distribution.properties");
                            jPushHelper.sendMsgByAlias(Message.newBuilder().setMsgContent("").addExtras(map).build(), deliveryClerk.getAccount());
                        } else {
                            System.out.println("sssssssssssssssssssss");
                            UserPushMsgUtil.reminderOrderPushByOrderStateToBusiness(orderId, userId, bussiness.getAccount(), order.getOrderNumber(), order.getOrderType());
                            JPushHelper companyJPushHelper = new JPushHelper("business.properties");
                            companyJPushHelper.sendMsgByAlias(Message.newBuilder().setMsgContent("").addExtras(map).build(), bussiness.getAccount());
                        }
                    } else if (order.getOrderState() == 12) {
                        UserPushMsgUtil.reminderOrderPushByOrderStateToAll(orderId, userId, bussiness.getAccount(), deliveryClerk.getAccount(), order.getOrderNumber(), order.getOrderType());
                        JPushHelper jPushHelper = new JPushHelper("distribution.properties");
                        JPushHelper companyJPushHelper = new JPushHelper("business.properties");
                        companyJPushHelper.sendMsgByAlias(Message.newBuilder().setMsgContent("").addExtras(map).build(), bussiness.getAccount());
                        jPushHelper.sendMsgByAlias(Message.newBuilder().setMsgContent("").addExtras(map).build(), deliveryClerk.getAccount());
                    }
                    flag = true;
                } else {
                    throw new ServiceException("您已催单请耐心等待！");
                }
            }

        }

        return flag;
    }

    /**
     * 根据订单编号判断是否催单
     *
     * @param orderId 订单编号
     * @return
     */
    @Override
    public Integer checkIsReminder(Integer orderId) {
        Reminder reminder = reminderDao.getEntityByOrderId(orderId);
        if (reminder != null) {
            return 1;
        }
        return 0;
    }

    /**
     * 根据订单编号判断商家是否被催单
     *
     * @param orderId 订单编号
     * @return
     */
    @Override
    public Integer checkBussinessIsReminder(Integer orderId) {
        Reminder reminder = reminderDao.getEntityByOrderId(orderId);
        if (reminder != null && reminder.getBussIsLook() != 1) {
            return 1;
        }
        return 0;
    }

    /**
     * 根据订单编号获取催单信息
     *
     * @param orderId 订单编号
     * @return
     */
    @Override
    public Reminder getReminderByOrderId(Integer orderId) {
        return reminderDao.getEntityByOrderId(orderId);
    }

    /**
     * 修改被催单查看状态
     *
     * @param reminderId 催单编号
     * @return
     */
    @Override
    public boolean updateRemindLook(Integer reminderId) throws ServiceException {
        Reminder reminder = reminderDao.getEntityById(reminderId);
        if (reminder == null) {
            throw new ServiceException("当前订单未被催单");
        }
        if (reminder.getBussIsLook() == 1) {
            throw new ServiceException("当前催单已经被查看");
        }
        return reminderDao.updateRemindLook(reminderId) > 0;
    }

    @Override
    public boolean updateReminderState(String orderNumber, int disId) {
        return reminderDao.updateReminderState(orderNumber);
    }

    @Override
    public int checkIsReminderStatus(Integer id) {

        return reminderDao.checkIsReminderStatus(id);
    }
}
